/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmx.repoDomain.repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lmx.common.constants.CommonConstants;
import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.CustomerService;
import com.lmx.persistence.service.DeviceService;
import com.lmx.persistence.service.WeiXinService;
import com.lmx.repoDomain.exception.BusinessException;
import com.lmx.repoDomain.tmodel.Customer;
import com.lmx.repoDomain.tmodel.CustomerDrug;
import com.lmx.repoDomain.tmodel.Device;
import com.lmx.repoDomain.tmodel.EvaluateModel;
import com.lmx.repoDomain.tmodel.NurseGroup;

import io.katharsis.queryParams.QueryParams;
import io.katharsis.repository.annotations.JsonApiFindAll;
import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;

@JsonApiResourceRepository(Device.class)
@Component
public class DeviceRepository extends BaseRepository<Device>{
	
	@Autowired
	private CustomerService customerService; 
	
	@Autowired
	private DeviceService deviceService; 

    @PostConstruct
    public void init() {
        dslJpaService.setEntityClass(Device.class); 
    }

    
	@Override
	@JsonApiSave
	public Device save(Device model) {
		if (model.getId() == null) {
			Device temp = customerService.getDeviceBySeq(model);
			if (temp == null) {
				model = super.save(model);
			} else {
				model.setErrcode(4);// 名字重复
				throw new BusinessException(null, CommonConstants.ERROR_CODE.NAME_ERROR.label(), null);
			}
		} else {
			if (model.getDelStatus() == 1) {// 删除
				//判断已绑定提示不能删除
				int deleteFlag = deviceService.checkDeviceRelations(model);
				if (deleteFlag > 0) {
					model.setErrcode(8);// no delete
					throw new BusinessException(null, CommonConstants.ERROR_CODE.NO_DELETE.label(), null);
				}
				dslJpaService.update(model);
			} else {
				if (model.getOperateFlag() != null && ((CommonConstants.DEVICE_OPERATE_CODE.BIND.label().equals(model.getOperateFlag())
						|| (CommonConstants.DEVICE_OPERATE_CODE.REMOVE_BIND.label().equals(model.getOperateFlag()))))) {// 进行解绑或者绑定操作
					deviceService.operateDeviceByFlag(model);
				} else {// 修改device信息
					Device temp = customerService.getDeviceBySeqFilterId(model);
					if (temp == null) {
						dslJpaService.update(model);
					} else {
						model.setErrcode(4);// 名字重复
						throw new BusinessException(null, CommonConstants.ERROR_CODE.NAME_ERROR.label(), null);
					}
				}
				if (model.getOperateFlag() != null && ("reset".equals(model.getOperateFlag()))) {// 复位操作
					deviceService.resetDevice(model);
				}
			}
		}
		return model;
	}
   

	@Override
	@JsonApiFindAll
	public Iterable<Device> findAll(final QueryParams requestParams) {
		Map<String, String> keyMap = CommonUtil.getKeyMapByFilter(requestParams);
		Iterable<Device> deviceList = new ArrayList<>();
		List dataList = new ArrayList<>();
		String queryData = "";
		String queryType = "";
		String typeCode = "";  // 硬件方案  code
		Short deviceStatus=-1;//  1  可用  0  不可用
		String deviceType="";//   设备类型的 code
		if (keyMap.get("type.codeInfo") != null) {//查询条件
			typeCode = keyMap.get("type.codeInfo");
		}
		if (keyMap.get("queryData") != null) {//查询条件
			queryData = keyMap.get("queryData");
		}
		if (keyMap.get("queryType") != null) {//查询类型
			queryType = keyMap.get("queryType");
		}
		if (keyMap.get("deviceStatus") != null&&keyMap.get("deviceStatus").matches("\\d+")) {//查询条件
			deviceStatus =Short.valueOf(keyMap.get("deviceStatus"));
		}
		if (keyMap.get("deviceType.typecode") != null) {
			deviceType = keyMap.get("deviceType.typecode");
		}
		if ("queryAll".equals(queryType)) {
			// 搜索device from Customer Employee Room bed
			dataList = deviceService.getDeviceFromAll(queryData,deviceStatus,deviceType,typeCode);
			commonService.setPageInfo(dataList, requestParams);
			deviceList = dataList;
		} else {
			dslJpaService.setEntityClass(Device.class);
			return super.findAll(requestParams);
		}
		return deviceList;
	}
	
}
